package applications.studentApplicationForm.fc.lib;

import java.util.Observable;

import ontocompo.api.OntoCompoIntrospection;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;

import applications.studentApplicationForm.fc.api.AcademicRecord;
import applications.studentApplicationForm.fc.api.Address;
import applications.studentApplicationForm.fc.api.Semester;
import applications.studentApplicationForm.fc.api.StudentForm;
import applications.studentApplicationForm.fc.api.StudentFormDirectoryService;
import applications.studentApplicationForm.fc.api.Subject;


@Component(provides = {@Interface(name = "studentformdirectoryservice", signature = applications.studentApplicationForm.fc.api.StudentFormDirectoryService.class), @Interface(name = "observable", signature = ontocompo.api.Observable.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})

public class StudentFormDirectoryServiceImpl extends Observable implements StudentFormDirectoryService,ontocompo.api.Observable, OntoCompoIntrospection {


	private String studentNumber;
	private String year;

	private String uid;
	private org.objectweb.fractal.api.Component c;

	private StudentForm[] students;



	public StudentFormDirectoryServiceImpl() {
		this.uid = "applications.studentApplicationForm.fc.lib.StudentFormDirectoryServiceImpl";

		students = new StudentForm[2];
		students[0] =  buildFirstStudent();
		students[1] =  buildSecondStudent();


	}


	@Override
	public StudentForm getStudentForm() {

		if (studentNumber.equals("12345"))
		{
			return students[0];
		}
		else
		{
			if (studentNumber.equals("56789")) 
			{

				return students[1];
			}
			else
			{
				return null;
			}
		}
	}



	// annees

	@Override
	public String[][] getYearData() {

		// 4 annees
		// 4 choses a afficher; year, code, university name, grade
		String[][] data = new String[4][4];

		StudentForm studentForm = getStudentForm();

		if (studentForm !=null)
		{
			AcademicRecord[] academicRecords = studentForm.getAcademicRecord();

			int m=0;
			for (int k = 0; k<academicRecords.length; k++)
			{
				String [] yearData = {
						academicRecords[k].getYear(),
						academicRecords[k].getCode(),
						academicRecords[k].getUniversityName(),
						academicRecords[k].getGradeName()
				};

				data[m++] = yearData;
			}


			return data;

		}

		else{

			return null;
		}
	}


	@Override
	public String[] getYearTitles() {

		String[] yearTitles = {"Year","Code","University name","Grade name"};

		return yearTitles;
	}





	// semestres

	@Override
	public String[][] getSemesterData(String year) {

		//2 = 2 semestres
		// 3 = on a 3 colonnes a afficher, semester's name, average , validation

		String[][] data = new String[2][3];

		StudentForm studentForm = getStudentForm();

		if (studentForm !=null)
		{
			AcademicRecord[] academicRecords = studentForm.getAcademicRecord();

			for (AcademicRecord academicRecord : academicRecords)
			{
				if (academicRecord.getYear().equals(year))
				{

					for (int k=0; k<academicRecord.getSemester().length; k++)
					{

						String[] dataSemester =  {
								academicRecord.getSemester()[k].getName(),
								academicRecord.getSemester()[k].getSemesterAverage(),
								academicRecord.getSemester()[k].getValidationSemester(),
						};

						data[k] = dataSemester;
					}

				}
			}


			return data;

		}

		else{

			return null;
		}


	}


	@Override
	public String[] getSemesterTitles() {

		String[] semesterTitles = {"semester's name","semester's average","validation semester"};
		return semesterTitles;
	}




	// matieres

	@Override
	public String [] getSubjectTitles() {

		String [] subjectTitle = {"subjectCode","subjectName","subjectNote","subjectValidation"};
		return subjectTitle;

	}



	@Override
	public String [][] getSubjectData(String year){

		// 6 matieres par semestre
		// 4 colonnes a afficher; code matiere, note, name, validation

		String[][] data = new String[6][4];
		int k =0;
		int j=0;

		StudentForm studentForm = getStudentForm();

		if (studentForm !=null)
		{
			AcademicRecord[] academicRecords = studentForm.getAcademicRecord();

			for (AcademicRecord academic : academicRecords)
			{
				// on retrouve l'annee voulue
				if (academic.getYear().equals(year))
				{
					int m = 0;
					// pour chaque semestre
					for (k=0; k<academic.getSemester().length; k++)
					{
						// on affiche ses matieres
						for(j=0; j<academic.getSemester()[k].getSubject().length; j++)
						{

							String[] subjectData = {
									academic.getSemester()[k].getSubject()[j].getSubjectCode(),
									academic.getSemester()[k].getSubject()[j].getSubjectName(),
									academic.getSemester()[k].getSubject()[j].getSubjectNote(),
									academic.getSemester()[k].getSubject()[j].getSubjectValidation()


							};

							data[m++] = subjectData;

						}

					}
				}
			}

			return data;

		}

		else
		{

			return null;
		}

	}



	// on envoie des notifications au cas ou on a change le student number
	@Override
	public void setStudentNumber(String studentNumber) {
		this.studentNumber = studentNumber;
		setChanged();
		notifyObservers();
	}

	public void clearChanged() {
		super.clearChanged();
	}

	public void setChanged() {
		super.setChanged();
	}


	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}


	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}


	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}


	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;

	}


	@Override
	public void setYearValue(String year) {
		// TODO Auto-generated method stub
		this.year = year;
		setChanged();
		notifyObservers();

	}


	@Override
	public String getYearValue() {
		// TODO Auto-generated method stub
		return this.year;
	}


	@Override
	public String getStudentNumber() {
		// TODO Auto-generated method stub
		return this.studentNumber;
	}
	
	


	private StudentForm buildSecondStudent() {


		Address adress = new AddressImpl("4 rue Albert 1er", "06200", "FRANCE", "0789908709");


		// sujet 1ere annee  = s1+s2
		Subject subjectMaths = new SubjectImpl("L1Geo", "Geographie", "2", "Ajourne");
		Subject subjectJava = new SubjectImpl("L1Hist", "Histoire", "19", "Acquis");
		Subject subjectProg = new SubjectImpl("L1Maths", "Maths", "13", "Acquis");


		Subject subjectEEA = new SubjectImpl("L1Com", "Communication", "7", "Ajourne");
		Subject subjectC = new SubjectImpl("L1Bio", "Biologie", "3", "Ajourne");
		Subject subjectAnglais = new SubjectImpl("L1Anglais", "Anglais", "12", "Acquis");


		Subject[] subjects1 = new Subject[]{subjectMaths,subjectJava,subjectProg};
		Subject[] subjects2 =  new Subject[]{subjectEEA,subjectC,subjectAnglais};





		// semestres de la 1ere annee
		Semester semester1 = new SemesterImpl("Licence 1 Geographie s1", "12", "acquis", subjects1);
		Semester semester2 = new SemesterImpl("Licence 1 Geographie s2", "8", "Ajourne", subjects2);




		// sujet 2eme annee = s3+s4

		Subject subjectMathsL2 = new SubjectImpl("L2Hist", "Histoire de la geographie", "18", "Acquis");
		Subject subjectJavaL2 = new SubjectImpl("L2Maths", "Maths appliques", "19", "Ajourne");
		Subject subjectProgL2 = new SubjectImpl("L2Physique", "Physique ", "3", "AJ");


		Subject subjectEEAL2 = new SubjectImpl("L2EEA", "EEA ", "17", "Ajourne");
		Subject subjectCL2 = new SubjectImpl("L2Com", "Communication", "3", "Acquis");
		Subject subjectAnglaisL2 = new SubjectImpl("L2Angl", "Anglais avance", "12", "Acquis");

		Subject[] subjects3 = new Subject[]{subjectMathsL2,subjectJavaL2,subjectProgL2};
		Subject[] subjects4 =  new Subject[]{subjectEEAL2,subjectCL2,subjectAnglaisL2};

		// semestres de la 2eme annee

		Semester semester3 = new SemesterImpl("Licence 2 Geographie s3", "14", "acquis", subjects3);
		Semester semester4 = new SemesterImpl("Licence 2 Geographie s4", "18", "acquis", subjects4);



		// sujet 3eme annee = s3+s4

		Subject subjectMathsL3 = new SubjectImpl("L3Maths", "Maths ", "18", "Acquis");
		Subject subjectJavaL3 = new SubjectImpl("L3Sci", "Sciences", "19", "Ajourne");
		Subject subjectProgL3 = new SubjectImpl("L3Hist", "Histoire", "3", "AJ");


		Subject subjectEEAL3 = new SubjectImpl("L3Maths", "Maths", "17", "Ajourne");
		Subject subjectCL3 = new SubjectImpl("L3Art", "Art", "3", "Acquis");
		Subject subjectAnglaisL3 = new SubjectImpl("L3Anglais", "Anglais avance", "12", "Acquis");



		Subject[] subjects5 = new Subject[]{subjectMathsL3,subjectJavaL3,subjectProgL3};
		Subject[] subjects6 =  new Subject[]{subjectEEAL3,subjectCL3,subjectAnglaisL3};

		// semestres de la 3eme annee

		Semester semester5 = new SemesterImpl("Licence 3 Geographie s5", "13", "acquis", subjects5);
		Semester semester6 = new SemesterImpl("Licence 3 Geographie s6", "17", "acquis", subjects6);



		// sujet 4eme annee = s3+s4

		Subject subjectMathsL4 = new SubjectImpl("M1Art", "Art", "18", "Acquis");
		Subject subjectJavaL4 = new SubjectImpl("M1hist", "Histoire", "19", "Ajourne");
		Subject subjectProgL4 = new SubjectImpl("M1Maths", "Maths", "3", "AJ");


		Subject subjectEEAL4 = new SubjectImpl("M1Geo", "Geographie", "17", "Ajourne");
		Subject subjectCL4 = new SubjectImpl("M1Maths", "Maths", "3", "Acquis");
		Subject subjectAnglaisL4 = new SubjectImpl("M1MAnglais", "Anglais avance", "12", "Acquis");



		Subject[] subjects7 = new Subject[]{subjectMathsL4,subjectJavaL4,subjectProgL4};
		Subject[] subjects8 =  new Subject[]{subjectEEAL4,subjectCL4,subjectAnglaisL4};




		// semestres de la 4eme annee

		Semester semester7 = new SemesterImpl("Master 1 Geographie s7", "10.5", "acquis", subjects7);
		Semester semester8 = new SemesterImpl("Master 1 Geographie s8", "11", "acquis", subjects8);




		Semester[] semesterL1 = new Semester[]{semester1,semester2};
		Semester[] semesterL2 = new Semester[]{semester3,semester4};

		Semester[] semesterL3 = new Semester[]{semester5,semester6};
		Semester[] semesterMaster1 = new Semester[]{semester7,semester8};





		AcademicRecord academicRecordL1 = new AcademicRecordImpl("2007/2008", "SL1Geo", "Universite de Nice","Licence 1 Geo","UFR Lettres", semesterL1);
		AcademicRecord academicRecordL2 = new AcademicRecordImpl("2008/2009", "SL2Geo", "Universite de Nice","Licence 2 Geo", "UFR Lettres",semesterL2);
		AcademicRecord academicRecordL3 = new AcademicRecordImpl("2009/2010", "SL3Geo", "Universite de Nice","Licence 3 Geo","UFR Lettres", semesterL3);
		AcademicRecord academicRecordMaster1 = new AcademicRecordImpl("2010/2011", "SM1Geo", "Universite de Nice","Master 1 Geo","UFR Lettres", semesterMaster1);


		AcademicRecord[] academicRecordList = new AcademicRecord[]{academicRecordL1,academicRecordL2,academicRecordL3,academicRecordMaster1};



		StudentForm student = new StudentFormImpl("Female", "56789", "2508909", "Damas", "Jeanne", "07/09/1990", 
				"jeanne@gmail.com", "francaise", adress, academicRecordList);


		return student;

	}
	
	
	
	private StudentForm buildFirstStudent() {

		Address adress = new AddressImpl("22 rue Magnan", "06000", "FRANCE", "0612345678");


		// sujet 1ere annee  = s1+s2
		Subject subjectMaths = new SubjectImpl("L1Maths", "Maths", "12", "Acquis");
		Subject subjectJava = new SubjectImpl("L1Java", "Java", "9", "Ajourne");
		Subject subjectProg = new SubjectImpl("L1Reseaux", "Reseaux", "13", "Acquis");


		Subject subjectEEA = new SubjectImpl("L1EEA", "Electronique", "7", "Ajourne");
		Subject subjectC = new SubjectImpl("L1C", "Langage C", "3", "Ajourne");
		Subject subjectAnglais = new SubjectImpl("L1C#", "C#", "12", "Acquis");


		Subject[] subjects1 = new Subject[]{subjectMaths,subjectJava,subjectProg};
		Subject[] subjects2 =  new Subject[]{subjectEEA,subjectC,subjectAnglais};





		// semestres de la 1ere annee
		Semester semester1 = new SemesterImpl("Licence 1 Informatique s1", "12", "acquis", subjects1);
		Semester semester2 = new SemesterImpl("Licence 1 Informatique s2", "8", "Ajourne", subjects2);




		// sujet 2eme annee = s3+s4

		Subject subjectMathsL2 = new SubjectImpl("L2Chimie", "Chimie", "18", "Acquis");
		Subject subjectJavaL2 = new SubjectImpl("L2Java", "Java", "19", "Ajourne");
		Subject subjectProgL2 = new SubjectImpl("L2C++", "C++ ", "3", "AJ");


		Subject subjectEEAL2 = new SubjectImpl("L2EEA", "Electronique", "17", "Ajourne");
		Subject subjectCL2 = new SubjectImpl("L2SCHEME", "SCHEME", "3", "Acquis");
		Subject subjectAnglaisL2 = new SubjectImpl("L2Anglais", "Anglais ", "12", "Acquis");

		Subject[] subjects3 = new Subject[]{subjectMathsL2,subjectJavaL2,subjectProgL2};
		Subject[] subjects4 =  new Subject[]{subjectEEAL2,subjectCL2,subjectAnglaisL2};

		// semestres de la 2eme annee

		Semester semester3 = new SemesterImpl("Licence 2 Informatique s3", "14", "acquis", subjects3);
		Semester semester4 = new SemesterImpl("Licence 2 Informatique s4", "18", "acquis", subjects4);



		// sujet 3eme annee = s3+s4

		Subject subjectMathsL3 = new SubjectImpl("L3Maths", "Maths ", "18", "Acquis");
		Subject subjectJavaL3 = new SubjectImpl("L3Java", "Java 2EE", "19", "Ajourne");
		Subject subjectProgL3 = new SubjectImpl("L3Com", "Communication", "3", "AJ");


		Subject subjectEEAL3 = new SubjectImpl("L3MEEA", "EEA ", "17", "Ajourne");
		Subject subjectCL3 = new SubjectImpl("L3C++", "C++", "3", "Acquis");
		Subject subjectAnglaisL3 = new SubjectImpl("L3Anglais", "Anglais ", "12", "Acquis");



		Subject[] subjects5 = new Subject[]{subjectMathsL3,subjectJavaL3,subjectProgL3};
		Subject[] subjects6 =  new Subject[]{subjectEEAL3,subjectCL3,subjectAnglaisL3};

		// semestres de la 3eme annee

		Semester semester5 = new SemesterImpl("Licence 3 Informatique s5", "13", "acquis", subjects5);
		Semester semester6 = new SemesterImpl("Licence 3 Informatique s6", "17", "acquis", subjects6);



		// sujet 4eme annee = s3+s4

		Subject subjectMathsL4 = new SubjectImpl("M1Maths", "Maths ", "18", "Acquis");
		Subject subjectJavaL4 = new SubjectImpl("M1Java", "Java 2EE", "19", "Ajourne");
		Subject subjectProgL4 = new SubjectImpl("M1C#", "C#", "3", "AJ");


		Subject subjectEEAL4 = new SubjectImpl("M1EEA", "EEA ", "17", "Ajourne");
		Subject subjectCL4 = new SubjectImpl("M1C++", "C++", "3", "Acquis");
		Subject subjectAnglaisL4 = new SubjectImpl("M1Anglais", "Anglais ", "12", "Acquis");



		Subject[] subjects7 = new Subject[]{subjectMathsL4,subjectJavaL4,subjectProgL4};
		Subject[] subjects8 =  new Subject[]{subjectEEAL4,subjectCL4,subjectAnglaisL4};




		// semestres de la 4eme annee

		Semester semester7 = new SemesterImpl("Master 1 info s7", "10.5", "acquis", subjects7);
		Semester semester8 = new SemesterImpl("Master 1 info s8", "11", "acquis", subjects8);




		Semester[] semesterL1 = new Semester[]{semester1,semester2};
		Semester[] semesterL2 = new Semester[]{semester3,semester4};

		Semester[] semesterL3 = new Semester[]{semester5,semester6};
		Semester[] semesterMaster1 = new Semester[]{semester7,semester8};





		AcademicRecord academicRecordL1 = new AcademicRecordImpl("2007/2008", "SL1I", "Universite de Nice","Licence 1 info","UFR SCIENCES", semesterL1);
		AcademicRecord academicRecordL2 = new AcademicRecordImpl("2008/2009", "SL2I", "Universite de Nice","Licence 2 info", "UFR SCIENCES",semesterL2);
		AcademicRecord academicRecordL3 = new AcademicRecordImpl("2009/2010", "SL3I", "Universite de Nice","Licence 3 info","UFR SCIENCES", semesterL3);
		AcademicRecord academicRecordMaster1 = new AcademicRecordImpl("2010/2011", "SM1MIA", "Universite de Nice","Master 1 info","UFR SCIENCES", semesterMaster1);


		AcademicRecord[] academicRecordList = new AcademicRecord[]{academicRecordL1,academicRecordL2,academicRecordL3,academicRecordMaster1};



		StudentForm studentForm = new StudentFormImpl("Male", "12345", "2508909", "Dupond", "Jean", "07/09/1988", 
				"dupond@gmail.com", "francaise", adress, academicRecordList);


		return studentForm;
	}




}
